function DATA = ChangeInconsistency(DATA, problem_indicies)
% This function is to change the field names to make it consistent with the comparison
FN = fieldnames(DATA);
N = length(problem_indicies);
for i = 1 : N   
    index = problem_indicies(i) + 1 ;   
    frame =  sprintf('mbs_frame%06d', index);
    
    CurrentFrame = DATA.(frame);
 
    if isfield(CurrentFrame, 'timestep')
        DATA.(frame).step_size = DATA.(frame).timestep;
    end
    % bodies 
    BODIES = CurrentFrame.bodies;
    %DATA.(frame).bodies = removeDead(BODIES);
    if isfield(BODIES, 'world_inertia_tensors')
        DATA.(frame).bodies.inertias =  DATA.(frame).bodies.world_inertia_tensors;
    end
    if isfield(BODIES, 'mass')
        DATA.(frame).bodies.masses = DATA.(frame).bodies.mass;
    end
    if isfield(BODIES, 'state')
        DATA.(frame).bodies.states = DATA.(frame).bodies.state;
    end
    if isfield(BODIES, 'inertia')
        DATA.(frame).bodies.inertias = DATA.(frame).bodies.inertia;
    end
    % contacts
    CONTACTS = CurrentFrame.contacts;
    if isfield(CONTACTS, 'gaps')
        DATA.(frame).contacts.PSI = DATA.(frame).contacts.gaps;
    end
    if isfield(CONTACTS, 'gap')
        DATA.(frame).contacts.PSI = DATA.(frame).contacts.gap;
    end
    if isfield(CONTACTS, 'mu_s')
        DATA.(frame).contacts.mu = DATA.(frame).contacts.mu_s;
    end
    DATA.(frame) = CheckDimension(DATA.(frame));
end
end

% function bodies = removeDead(BODIES)
% state = BODIES.state;
% end
function [obj] = CheckDimension(obj)
% BODIES
[m, n] = size(obj.bodies.forces);
if m==1 && m < n
    obj.bodies.forces = obj.bodies.forces';
    gravity = [0; 0; -9.80665];
    gravity = repmat(gravity, length(obj.bodies.forces)/3, 1);
    obj.bodies.forces = obj.bodies.forces + gravity;
end

[m, n] = size(obj.bodies.ids);
if m==1 && m < n
    obj.bodies.ids = obj.bodies.ids';
end

[m, n] = size(obj.bodies.linear_damping_force);
if m==1 && m < n
    obj.bodies.linear_damping_force = obj.bodies.linear_damping_force';
end

[m, n] = size(obj.bodies.masses);
if m==1 && m < n
    obj.bodies.masses = obj.bodies.masses';
end

[m, n] = size(obj.bodies.states);
if m==1 && m < n
    obj.bodies.states = obj.bodies.states';
end

[m, n] = size(obj.bodies.velocities);
if m==1 && m < n
    obj.bodies.velocities = obj.bodies.velocities';
end

[m, n] = size(obj.bodies.inertias);
if m == 3  &&  n ~= 3
    obj.bodies.inertias = obj.bodies.inertias';
end
%[m, n] = size(obj.bodies.body_inertia_tensors);
%if m == 3  &&  n ~= 3
%    obj.bodies.body_inertia_tensors = obj.bodies.body_inertia_tensors';
%end
[m, n] = size(obj.bodies.positions);
if m == 3  &&  n ~= 3
    obj.bodies.positions = obj.bodies.positions';
end
[m, n] = size(obj.bodies.inertias);
if m == 3  &&  n ~= 3
    obj.bodies.inertias = obj.bodies.inertias';
end   
 
[m, n] = size(obj.bodies.quaternions);
if m == 4  &&  n ~= 4
    obj.bodies.quaternions = obj.bodies.quaternions';
end


% CONTACTS
[m, n] = size(obj.contacts.PSI);
if m == 1  &&  n > 1
    obj.contacts.PSI = obj.contacts.PSI';
end

%[m, n] = size(obj.contacts.gaps);
% if m == 1  &&  n > 1
%     obj.contacts.gaps = obj.contacts.gaps';
% end

[m, n] = size(obj.contacts.mu);
if m == 2  &&  n ~= 2
    obj.contacts.mu = obj.contacts.mu';
end

[m, n] = size(obj.contacts.normals);
if m == 3 && n ~= 3
    obj.contacts.normals = obj.contacts.normals';
end

[m, n] = size(obj.contacts.pairs);
if m == 2 && n~= 2
    obj.contacts.pairs = obj.contacts.pairs';
end

[m, n] = size(obj.contacts.points);
if m == 3 && n~= 3
    obj.contacts.points = obj.contacts.points';
end
end
